<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="dicdetails" highlight-current-row v-loading="listLoading" stripe @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" width="200">
			</el-table-column>
			<el-table-column prop="name" label="明细名" width="300">
			</el-table-column>
			<el-table-column prop="dicType.name" label="数据字典" width="250">
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :total="total"
						   :current-page="currentPage"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--
			:visible.sync：控制是否显示模态框，true显示，false不显示
			:rules：绑定一个表单验证规则
			ref：给表单取一个别名
			prop：表单验证名称
		-->
		<!--新增/编辑界面-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="120px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="明细名" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="数据字典">
					<el-select v-model="saveForm.dicType" value-key="id" placeholder="请开始你的表演">
						<!--
							:key---唯一表示符
							:label---下拉选项框中从服务端获取到的值
							:value---返回给模型层的数据
							item---迭代得到的对象(如果要向模型层返回对象，则需要在) el-select必须在标签中添加value-key，值为唯一表示符
						-->
						<el-option
								v-for="item in dictypes"
								:key="item.value"
								:label="item.name"
								:value="item">
							<span style="float: left">{{ item.name }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
    import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    import { getDicdetailListPage, removeDicdetail, batchRemoveDicdetail, editDicdetail, addDicdetail } from '../../api/api';

    export default {
        data() {
            return {
                title:"",
                filters: {
                    keyword: ''
                },
                dicdetails: [],
                total: 0,
                currentPage: 1,
                pageSize: 5,
                listLoading: false,

                saveFormVisible: false,//新增界面是否显示
                saveLoading: false,
                saveFormRules: {
                    name: [
                        { required: true, message: '请输入明细名', trigger: 'blur' }
                    ],
                    dicType: [
                        { required: true, message: '请选择数据字典', trigger: 'change' }
                    ]
                },
                // 获取员工信息
                getDicTypes(){
                    this.$http.get("/dic/list")
                        .then(result=>{
                            this.dictypes = result.data;
                        })
                        .catch(result=>{

                        })
                },
                // 新增界面数据
                saveForm: { // 置空表单数据
                    id: null, // 新增时id为空
                    name: '',
					dicType:null
                }

            }
        },
        methods: {
            search(){ // 高级查询，一定定位到第一页
                this.currentPage = 1;
                this.getDicdetails();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getDicdetails();
            },
            // 获取部门列表
            getDicdetails() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyword: this.filters.keyword,
                };
                this.listLoading = true; // 开启忙等框
                // 使用axios向后端localhost:8080发送请求，拿数据
                this.$http.post("/detail/page", para)
                    .then(result => {
                        this.listLoading = false; // 关闭忙等框
                        result = result.data;
                        this.dicdetails = result.rows;
                        this.total = result.total;
                    })
                    .catch(result => { // 操作失败时，向前端返回信息提示
                        console.log(result);
                        this.$message({
                            type: 'error',
                            message: `系统繁忙，程序猿正在快马加鞭的赶来！`
                        });
                    });
            },
            //NProgress.start();
            /*getDicdetailListPage(para).then((res) => {
                this.total = res.data.total;
                this.dicdetails = res.data.dicdetails;
                this.listLoading = false;
                //NProgress.done();
            });
        },*/
            // 删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true; // 开启忙等框
                    //NProgress.start();
                    // 向端发送delete请求，传递id删除数据
                    this.$http.delete("/detail/" + row.id)
                        .then(result=>{
                            result = result.data;
                            this.listLoading = false;
                            if(result.success){
                                this.$message({
                                    type: 'success',
                                    message: result.message
                                });
                                this.currentPage = 1;
                                // 删除成功，刷新页面
                                this.getDicdetails();
                            } else {
                                this.$message({
                                    type: 'error',
                                    message: result.message
                                });
                            }
                        })
                        .catch(result=>{
                            this.listLoading = false;
                            console.log(result);
                            this.$message({
                                type: 'error',
                                message: result.message
                            });
                        })
                })
            },
            // 显示编辑界面
            handleEdit: function (index, row) {
                this.title = "编辑";
                this.saveFormVisible = true;
                this.saveForm = Object.assign({}, row); // 克隆当前行数据并回显
                this.$refs['saveForm'].resetFields(); // 置空表单校验
            },
            //显示新增界面
            handleAdd: function () {
                this.title = "新增";
                this.saveFormVisible = true;
                this.saveForm = { // 置空表单数据
                    id: null, // 新增时id为空
                    name: '',
                    dicType: '',
                };
                this.$refs['saveForm'].resetFields();
            },
            // 新增或编辑
            saveSubmit: function () {
                this.$refs.saveForm.validate((valid) => { // 校验表单
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true; // 开启提交按钮忙等框
                            // NProgress.start();
                            let para = Object.assign({}, this.saveForm); // 这里是克隆表单信息
                            // 使用axios向后端发送post请求，进行新增或修改操作
                            this.$http.post("/detail/addOrUpdate", para) // 将表单信息传递给后台
                                .then(result=>{
                                    result = result.data;
                                    if(result.success){
                                        this.$message({
                                            type: 'success',
                                            message: result.message
                                        });
                                        // 刷新页面
                                        this.$refs['saveForm'].resetFields(); // 重置表单校验
                                        this.saveFormVisible = false; // 关闭模态框
                                        this.getDicdetails();
                                    }else {
                                        this.$message({
                                            type: 'error',
                                            message: result.message
                                        });
                                    }
                                })
                                .catch(result=>{
                                    this.editLoading = false; // 关闭模态框
                                    console.log(result);
                                    this.$message({
                                        type: 'error',
                                        message: result.message
                                    });
                                })
                            /*para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            editDicdetail(para).then((res) => {
                                this.editLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getDicdetails();
                            });*/
                        });
                    }
                });
            },
        },
        mounted() {
            this.getDicdetails();
            this.getDicTypes();
        }
    }

</script>

<style scoped>

</style>